package cn.fairy.controller;

import cn.fairy.domain.User;
import cn.fairy.handler.UserFallBackHandler;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope  //刷新配置
public class UserController {
    @Value("${test}")
    private Long test;

    //限流降级
    @SentinelResource(value="getUserById",
            blockHandler="getUserByIdBlockHandler",
            blockHandlerClass = UserFallBackHandler.class,
            fallback = "getUserByIdFallBack")
    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Long id){

        return User.builder().id(test).name("不用管").intro("好看就行").build();

    }
    public static User getUserByIdFallBack(@PathVariable Long id ,BlockException ex){
        return User.builder().id(-2L).intro("降级了").name("fallback").build();
    }

}
